<template>
  <div class="chat-container">
    <div class="chat-messages">
      <div v-for="(msg, idx) in messages" :key="msg.role + '-' + idx" :class="['chat-message', msg.role]">
        <span class="role">{{ msg.role === 'user' ? '我' : 'AI' }}：</span>
        <span class="content" v-if="msg.role === 'user'">{{ msg.content }}</span>
        <span class="content" v-else>{{ msg.content }}</span>
      </div>
    </div>
    <div class="chat-input">
      <input v-model="input" @keyup.enter="sendMessage" placeholder="请输入你的问题..." />
      <button @click="sendMessage">发送</button>
    </div>
  </div>
</template>

<script setup>
import { ref, nextTick, reactive } from 'vue'
import axios from 'axios'

const input = ref('')
const messages = ref([])
const loading = ref(false)
const conversationId = ref(null)

function renderContent(msg) {
  if (msg.role === 'assistant') {
    // 仅做简单换行处理，排除 marked 问题
    return (msg.content || '').replace(/\n/g, '<br/>')
  }
  return msg.content
}

async function sendMessage() {
  if (!input.value.trim() || loading.value) return
  const question = input.value
  messages.value.push({ role: 'user', content: question })
  input.value = ''
  loading.value = true
  try {
    const payload = { question }
    if (conversationId.value) {
      payload.conversationId = conversationId.value
    }
    const res = await axios.post('/api/chat', payload)
    if (res.data) {
      // 打字机效果
      await typeWriterAI(res.data.answer)
      if (res.data.conversationId) {
        conversationId.value = res.data.conversationId
      }
    } else {
      messages.value.push({ role: 'assistant', content: '未获取到AI回复。' })
    }
  } catch (e) {
    messages.value.push({ role: 'assistant', content: '请求失败，请稍后重试。' })
  } finally {
    loading.value = false
  }
}

async function typeWriterAI(fullText) {
  const msg = reactive({ role: 'assistant', content: '' })
  messages.value.push(msg)
  for (let i = 0; i <= fullText.length; i++) {
    msg.content = fullText.slice(0, i)
    await nextTick()
    await new Promise(resolve => setTimeout(resolve, 12))
  }
}
</script>

<style scoped>
.chat-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  max-width: 1400px;
  margin: 0 auto;
  padding: 2rem;
  width: 100%;
  min-height: calc(100vh - 70px);
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 2rem;
  background: white;
  border-radius: 16px 16px 0 0;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.12);
  border: 1px solid rgba(0, 0, 0, 0.05);
  min-height: 400px;
}

.chat-message {
  margin-bottom: 1.5rem;
  display: flex;
  align-items: flex-start;
  gap: 1rem;
}

.chat-message.user {
  flex-direction: row-reverse;
}

.chat-message.assistant {
  flex-direction: row;
}

.chat-message .role {
  min-width: 50px;
  text-align: center;
  font-weight: 700;
  font-size: 0.9rem;
  padding: 0.5rem;
  border-radius: 50%;
  color: white;
  flex-shrink: 0;
}

.chat-message.user .role {
  background: linear-gradient(135deg, #3498db, #2980b9);
}

.chat-message.assistant .role {
  background: linear-gradient(135deg, #27ae60, #229954);
}

.chat-message .content {
  color: #2c3e50;
  background: #f8f9fa;
  border-radius: 16px;
  padding: 1rem 1.5rem;
  max-width: 70%;
  word-break: break-word;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  line-height: 1.6;
  border: 1px solid rgba(0, 0, 0, 0.05);
}

.chat-message.user .content {
  background: linear-gradient(135deg, #e3f2fd, #bbdefb);
  color: #1565c0;
}

.chat-message.assistant .content {
  background: linear-gradient(135deg, #f1f8e9, #dcedc8);
  color: #2e7d32;
}

.chat-input {
  display: flex;
  gap: 1rem;
  padding: 1.5rem 2rem;
  background: white;
  border-radius: 0 0 16px 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.12);
  border: 1px solid rgba(0, 0, 0, 0.05);
  border-top: none;
}

.chat-input input {
  flex: 1;
  padding: 1rem 1.5rem;
  border: 2px solid #e9ecef;
  border-radius: 12px;
  font-size: 1rem;
  transition: all 0.3s ease;
  background: #f8f9fa;
}

.chat-input input:focus {
  outline: none;
  border-color: #3498db;
  background: white;
  box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.1);
}

.chat-input button {
  padding: 1rem 2rem;
  background: linear-gradient(135deg, #3498db, #2980b9);
  color: white;
  border: none;
  border-radius: 12px;
  cursor: pointer;
  font-weight: 600;
  transition: all 0.3s ease;
  box-shadow: 0 4px 12px rgba(52, 152, 219, 0.3);
  white-space: nowrap;
}

.chat-input button:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(52, 152, 219, 0.4);
}

/* PC端响应式布局 */
@media (max-width: 1200px) {
  .chat-container {
    padding: 1.5rem;
  }

  .chat-messages {
    padding: 1.5rem;
  }

  .chat-input {
    padding: 1.2rem 1.5rem;
  }
}

@media (max-width: 992px) {
  .chat-container {
    padding: 1rem;
  }

  .chat-messages {
    padding: 1.2rem;
  }

  .chat-message .content {
    max-width: 80%;
  }

  .chat-input {
    padding: 1rem;
    gap: 0.8rem;
  }
}

@media (max-width: 768px) {
  .chat-message .content {
    max-width: 85%;
    padding: 0.8rem 1.2rem;
  }

  .chat-input {
    flex-direction: column;
    gap: 1rem;
  }

  .chat-input button {
    width: 100%;
  }
}
</style>